<?php
namespace Home\Controller;
use Think\Controller;
error_reporting(0);
header('content-type:text/html;charset=utf-8');
class IndexController extends Controller {
    public function index(){
        $this->display();

    }
//    品牌
    public function brand(){
        $brand=M('brand');
        $rs=$brand->select();           //查询所有输出模版
        $this->assign('rs',$rs);
        $this->display('brand');
    }
    public  function checkBrand(){
        if(IS_AJAX){
            $brand=M('brand');
           if(isset($_GET['brand'])){
//                添加品牌操作
               $data['brand']=$_GET['brand'];
               $data['class_id']=$_GET['class_id'];
               $rs=$brand->data($data)->add();
               if($rs){
                   echo "添加成功";
               }
           } else if(isset($_GET['id'])){
//               删除品牌操作
                $id=$_GET['id'];
               $rs=$brand->where("id=$id")->delete();
               if($rs) {
                   echo "删除成功";
               }
           }
        }else{
            $this->error("非法操作");
        }
    }
//手机型号
    public function type(){
        $type=M('type');
        $rs=$type->select();  //查询所有输出模版
        $this->assign('rs',$rs);
        $this->display();
    }
    public function checkType(){
        if(IS_AJAX){
            $type=M('type');
            if(isset($_GET['type'])){
//                添加品牌操作
                $data['type']=$_GET['type'];
                $data['brand_id']=$_GET['brand_id'];
                $rs=$type->data($data)->add();
                if($rs){
                    echo "添加成功";
                }
            } else if(isset($_GET['id'])){
//               删除品牌操作
                $id=$_GET['id'];
                $rs=$type->where("id=$id")->delete();
                if($rs) {
                    echo "删除成功";
                }
            }
        }else{
            $this->error("非法操作");
        }
    }
//    分类
    public function classes(){
        $classes=M('class');
        $rs=$classes->select();//查询所有输出模版
        $this->assign('rs',$rs);
        $this->display('classes');
    }
    public function checkClass(){
        if(IS_AJAX){
//            添加和删除商品
            $classes=M('class');
            if(isset($_GET['className'])){
                $className=$_GET['className'];
                $data['class']=$className;
                $rs=$classes->data($data)->add();

                if($rs){
                    echo "添加成功";
                }
            }else if(isset($_GET['id'])){
                $id=$_GET['id'];
                $rs=$classes->where("id=$id")->delete();
                if($rs){
                    echo "删除成功";
                }
            }
        }else{
            $this->error("非法操作") ;
        }
    }
//    型号评估
    public function mobile(){
        $this->display();
    }
    public function checkMobile(){
        if(IS_AJAX){
            if(isset($_GET['mobileName'])){             // 判断是否为添加大类操作
                $typeName=$_GET['mobileName'];
                $type=M('type');
                $rs=$type->where("type='{$typeName}'")->select();
                if(!$rs){
                    echo "输入的型号不存在，请到手机型号管理处添加";
                    exit;                                    //判断手机型号是否存在
                }
                $mobile=M();
                $mobile->execute("create table $typeName(id INT PRIMARY KEY auto_increment,fid INT(10),message VARCHAR(20),killed INT(10) DEFAULT 0)");
                $content = $_GET['classContent'];
                $content=explode('，',$content);            //将接收的字符串转化为数组
//                print_r($content);
                $i=count($content);
                $phone=M("{$typeName}");
                for($s=0;$s<$i;$s++){
                    $data['message']=$content[$s];
                    $data['fid']=0;                          //一级分类
                    $phone->data($data)->add();              //添加字段到数据表$typeName
                }
                $rs=count($phone->select());
                echo $rs;                                    //将添加的大分类条数返回到页面
            }else{
                $id=$_GET['id']-0+1;
                $content=$_GET['content'];
                $mobileName=$_GET['mobile'];
                $content=explode('，',$content);            //将接收的字符串转化为数组
                $i=count($content);
                $mobile=M("{$mobileName}");
                for($s=0;$s<$i;$s++){
                    $data['message']=$content[$s];
                    $data['fid']=$id;
                    $mobile->data($data)->add();
                }
                $rs=$mobile->where("fid=$id")->select();
                if($rs){
                    echo "添加成功";
                }
            }
        }else{
            $this->error('非法操作');
        }
    }
    public function test(){
        $this->display();
    }
    //修改品牌
    public function updateBrand(){
        if(IS_AJAX){
            $name=I('get.updateName');
            $id=I('get.id');
            $brand=M('brand');
            $brand->brand=$name;
            $rs=$brand->where("id=$id")->save();
            if($rs){echo "修改成功";}
        }else{
            $this->error('非法操作');
        }
    }
    //修改分类
    public function classUpdate(){
        if(IS_AJAX){
            $name=I('get.updateName');
            $id=I('get.id');
            $class=M('class');
            $class->class=$name;
            $rs=$class->where("id=$id")->save();
            if($rs){echo "修改成功";}
        }else{
            $this->error('非法操作');
        }
    }
    public function typeUpdate(){
        $type=M('type');
        $typer=$type->select();
        $this->assign('typer',$typer);
        $this->display();
    }

    public function typeUp(){
        $type=$_GET['type'];
        $mobile=M("$type");
        $rs=$mobile->select();
        $this->assign('rs',$rs);
        $this->assign('type',$type);
        $this->display();
    }
    public function typeUpCheck(){
        if(IS_AJAX){
//            if(isset($_GET['id'])){
//                $type=I('get.type');
//                $id=I('get.id');
//                $type=M($type);
//                $rs=$type->where("id=$id")->select();
//            }
            if(isset($_GET['tid'])){
                $id=I('get.tid');
                $type=I('get.type');
                $killed=I('get.killed');
                $message=I('get.message');
                $type=M($type);
                $type->message=$message;
                $type->killed=$killed;
                $rs=$type->where("id=$id")->limit(1)->save();
                echo $rs;
            }
            if(isset($_GET['fid'])){
                $fid=I('get.fid');
                $killed=I('get.killed');
                $message=I('get.message');
                $type=I('get.type');
                $type=M($type);
                $data['fid']=$fid;
                $data['message']=$message;
                $data['killed']=$killed;
                $rs=$type->add($data);
                echo $rs;
            }
        }
    }
    public function tyde(){
        if(IS_AJAX){
            $id=I('get.deleteId');
            $type=I('get.type');
            $type=M($type);
            $rs=$type->where("id=$id")->delete();
            echo $rs;
        }
    }

}
